package com.ymatou.iapi.couponmanage.testcase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.couponmanage.parameter.CreateBagBean;
import com.ymatou.iapi.couponmanage.parameter.createTaskByUserIdBean;
import com.ymatou.iapi.couponmanage.parameter.joinMsgBusBean;
import com.ymatou.iapi.couponmanage.service.CreateBagCall;
import com.ymatou.iapi.couponmanage.service.createTaskByUserIdCall;
import com.ymatou.iapi.couponmanage.service.joinMsgBusCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponServiceMCallServiceV2;
import com.ymttest.database.model.couponsendsubtask;
import com.ymttest.database.model.couponsendtask;
import com.ymttest.database.sqlwapper.couponsendsubtaskWapper;
import com.ymttest.database.sqlwapper.couponsendtaskWapper;
/**
*JOB补偿加入消息总线失败的子任务
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_joinMsgBus{
	private static joinMsgBusBean joinmsgbusBean;
	private static joinMsgBusCall joinmsgbusCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("JOB补偿加入消息总线失败的子任务");
	}

	@Before
	public void caseUp() {
		joinmsgbusBean=new joinMsgBusBean();
		joinmsgbusCall=new joinMsgBusCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(joinmsgbusCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_executeTask_001() {
		Logger.start(true,"JOB补偿加入消息总线失败的子任务-套券");
		try {
			CouponServiceMCallServiceV2 couponCallService = new CouponServiceMCallServiceV2();
			CreateBagBean createBagBean = couponCallService.setDefaultPlatformCreateCouponBagBeanForRecieve();
			CreateBagCall createBagCall = couponCallService.createPlatformCouponBag(createBagBean);
			
		    setCreateTaskByUserIdBeanForPlatform(createBagCall.getbagCode(),2);
			
			couponsendtask task=new couponsendtaskWapper().selectByCouponBatchCode(createBagCall.getbagCode()).get(0);
			int result=new couponsendsubtaskWapper().setStatusByTaskId(5);//把子任务状态设置为失败

			joinmsgbusCall.setData(joinmsgbusBean);
			joinmsgbusCall.callService();
			
			couponsendsubtask sub2=new couponsendsubtaskWapper().selectcouponsendsubtaskByTaskId(task.getTaskId()).get(0);
			Logger.verifyEquals("ok", joinmsgbusCall.getReturnData(), "验证返回ok");
			Logger.verifyEquals(4, sub2.getStatus(), "验证数据库couponsendsubtask表中的status=4,成功");
			Logger.comment("TaskId:"+sub2.getTaskId());
//			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	private void setCreateTaskByUserIdBeanForPlatform(String batchCode,int batchType ) throws ClientProtocolException, IOException
	{
		createTaskByUserIdBean createTaskByUserIdBean=new createTaskByUserIdBean();
		createTaskByUserIdCall call = new createTaskByUserIdCall();
		List<Integer> userIds=new ArrayList<Integer>();
		userIds.add(11);
		createTaskByUserIdBean.setUserIds(userIds);
		createTaskByUserIdBean.setBatchType(batchType);//1:生成私有券 2:生成短码 3:生成兑换码 4:生成套券
		createTaskByUserIdBean.setBatchCode(batchCode);
		createTaskByUserIdBean.setCreaterId(22);
		createTaskByUserIdBean.setCreaterName("autoTest");
		createTaskByUserIdBean.setNotify(false);
		call.setData(createTaskByUserIdBean);
		call.callService();
	}
}